<<template>
    <div class="needs-box">
        <div class="search3">
            <div style="display: flex;height: 35px;line-height: 35px;justify-content: space-around;">
                <el-input v-model="searchValue" maxlength="100" clearable style="width:290px;" />
                <img src="../assets/img/search.png" class="search-icon" @click="handleSearch" />&nbsp;&nbsp;
            </div>
            <div style="color:#696969;font-size:13px;font-style:normal;">&nbsp;热门搜索：
                <a class="tag-item" @click="handleTopicDetail('苹果')">苹果</a>
                <a class="tag-item" @click="handleTopicDetail('新疆哈密瓜')">新疆哈密瓜</a>
                <a class="tag-item" @click="handleTopicDetail('樱桃')">樱桃</a>
                <a class="tag-item" @click="handleTopicDetail('西红柿')">西红柿</a>
                <a class="tag-item" @click="handleTopicDetail('水稻')">水稻</a>
                <a class="tag-item" @click="handleTopicDetail('玉米')">玉米</a>
                <a class="tag-item" @click="handleTopicDetail('赣南脐橙')">赣南脐橙</a>
            </div>
            <el-button v-show="searchValue != ''" style="width: 80px;height:35px;line-height: 35px;margin-left: 10px;"
                @click="backHandle">
                <el-icon>
                    <Close />
                </el-icon>取 消
            </el-button>
        </div>
        <div class="purchase-content">
            <!-- //v-for为每个item放置在一个div(class=purchase-item)，:key指令用于为每个循环项分配一个唯一的键值。这里是将索引分配给key的 -->
            <div v-for="(item, index) in cneeds" :key="index" class="purchase-item" @click="detailsClick(item.orderId)">
                <div class="title">
                    <span class="tips">{{ item.type ===
                        'needs' ? '[求购]' : '' }}</span><!-- //如果购买项的类型为“needs”，则显示一个“[求购]”标记，否则不显示。 -->
                    <span class="title-content">{{ item.content }}</span>
                </div>
                <div class="content">{{ item.title }}</div>
                <div class="update-time">{{ time(item.updateTime) }}</div>
            </div>
        </div>
    </div>
</template>

    <script setup>
    import { defineProps, ref } from 'vue';
    import { useRouter } from 'vue-router';
    import { defineEmits } from 'vue';

    const emit = defineEmits(['handleSearch','refresh']);

    const router = useRouter();

    // 接收父组件传过来的数据
    const props = defineProps({
        cneeds: {
            type: Array,
        }
    })

    //格式化时间
    const time = (value) => {
        let date = new Date(value);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? "0" + MM : MM;
        let d = date.getDate();
        d = d < 10 ? "0" + d : d;
        let h = date.getHours();
        h = h < 10 ? "0" + h : h;
        let m = date.getMinutes();
        m = m < 10 ? "0" + m : m;
        let s = date.getSeconds();
        s = s < 10 ? "0" + s : s;
        return y + "-" + MM + "-" + d + " " + h + ":" + m;
    }
    //详情
    const detailsClick = (item) => {
        router.push(`/index/good_details?orderId=${item}`).catch((err) => err);
    }

    //搜索
    const searchValue = ref('');
    const handleSearch = () => {
        emit('handleSearch', searchValue.value)
    }
    const handleTopicDetail = (val) => {
        searchValue.value = val
        handleSearch()
    }
    //返回
    const backHandle = () => {
        searchValue.value = ''
        //调用父类的方法
        emit('refresh')
    }
</script>

    <style lang="scss" scoped>
    .needs-box {
        text-align: left;
        color: #222020;
        .search3 {
            display: flex;
            width: 1100px;
            background-color: white;
            padding: 10px 20px;
            margin: 0 auto;
            margin-top: 10px;
            margin-bottom: 10px;
            height: 60px;
            align-items: center;

            .tag-item {
                margin-right: 10px;
                cursor: pointer;
                color: blue;
            }

            .search-icon {
                position: relative;
                left: -2px;
                cursor: pointer;
            }
        }

        .purchase-content {
            background: #fff;
            padding: 20px;
            width: 1100px;
            margin: 0 auto;

            .purchase-item {
                display: flex;
                flex-direction: row;
                justify-content: space-between;
                padding: 10px 0;
                border-bottom: 1px solid #eee;

                .title {
                    width: 620px;
                    /*超出的部分隐藏*/
                    overflow: hidden;
                    /*文字用省略号替代超出的部分*/
                    text-overflow: ellipsis;
                    /*弹性伸缩盒子模型显示*/
                    display: -webkit-box;
                    /*限制在一个块元素显示文本的行数*/
                    -webkit-line-clamp: 1;
                    /*设置或检索伸缩盒对象的子元素排列方式*/
                    -webkit-box-orient: vertical;

                    .tips {
                        color: #999;
                    }

                    .title-content {
                        cursor: pointer;

                        &:hover {
                            color: #035D1C;
                            text-decoration: underline;
                        }
                    }
                }

                .content {
                    width: 300px;
                    margin-left: 20px;
                    /*超出的部分隐藏*/
                    overflow: hidden;
                    /*文字用省略号替代超出的部分*/
                    text-overflow: ellipsis;
                    /*弹性伸缩盒子模型显示*/
                    display: -webkit-box;
                    /*限制在一个块元素显示文本的行数*/
                    -webkit-line-clamp: 1;
                    /*设置或检索伸缩盒对象的子元 素排列方式*/
                    -webkit-box-orient: vertical;
                }

                .update-time {
                    width: 200px;
                    text-align: right;
                }
            }
        }
    }

    .search3 .el-input--suffix .el-input__inner {
        height: 35px;
        line-height: 35px;
    }
</style>